﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class Admin_BeBorrowedBooksRanking : System.Web.UI.Page
{
    public static string sqlpa = "";
    public string bardata, bardataname,linedata,linedataname,bingdata,bingname;
    protected void Page_Load(object sender, EventArgs e)
    {
        SQL sql = new SQL();
        DataSet ds = sql.getDataset("select distinct top 15  Book_info.BookID as 图书编号,Book_info.BookName as 图书条目,Book_info.Writer as 作者,BeBorrowTimes as 被借次数 from Reader_info, Book_info , Borrow_books_manage where Borrow_books_manage.bookid=Book_info.bookid and (LendBookDate is not null)  order by BeBorrowTimes desc", "");
        DataTable dt = new DataTable();
        DataRow drow;
        DataRowCollection drc;
        dt = ds.Tables[0];
        drc = dt.Rows;
        for (int i = 0; i < drc.Count; i++)
        {
            drow = drc[i];
            if (i!=drc.Count-1)
            {
                bardata += drow[3].ToString() + ",";
                bardataname += "\"" + drow[1].ToString() + "\",";
            } 
            else
            {
                bardata += drow[3].ToString() ;
                bardataname += "\"" + drow[1].ToString()+"\"";
            }
           
            
        }

        ds = sql.getDataset("select Typename,COUNT(Borrow_books_manage.BookID) from Borrow_books_manage,Book_info,Book_Type where Borrow_books_manage.BookID=Book_info.BookID and Book_Type.BookType=Book_info.BookType  GROUP BY Typename", "");
        dt = new DataTable();
        dt = ds.Tables[0];
        drc = dt.Rows;
        for (int i = 0; i < drc.Count; i++)
        {
            drow = drc[i];
            if (i != drc.Count - 1)
            {
                bingdata += "{label: \"" + drow[0].ToString() + "\",value: " + drow[1].ToString() + ", color: \"#" + GetRandomColor() + "\"},";
				                                      
            }
            else
            {
                bingdata += "{label: \"" + drow[0].ToString() + "\",value: " + drow[1].ToString() + ", color: \"#" + GetRandomColor() + "\"}";
            }


        }




    }
    protected void Cancel_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text=="")
        {
            sqlpa = "select distinct Book_info.BookID as 图书编号,Book_info.BookName as 图书条目,Book_info.Writer as 作者,BeBorrowTimes as 被借次数 from Reader_info, Book_info , Borrow_books_manage where Borrow_books_manage.bookid=Book_info.bookid and (LendBookDate between '" + date1.Value.ToString() + "' and '" + date2.Value.ToString() + "')  order by BeBorrowTimes desc";
            SqlDataSource1.SelectCommand = sqlpa;


            SqlDataSource1.SelectParameters["name"].DefaultValue = "1";
            SqlDataSource1.SelectParameters["date1"].DefaultValue = date1.Value.ToString();
            SqlDataSource1.SelectParameters["date2"].DefaultValue = date2.Value.ToString();
        } 
        else
        {

            sqlpa = "select distinct top " + Convert.ToInt32(TextBox1.Text) + "  Book_info.BookID as 图书编号,Book_info.BookName as 图书条目,Book_info.Writer as 作者,BeBorrowTimes as 被借次数 from Reader_info, Book_info , Borrow_books_manage where Borrow_books_manage.bookid=Book_info.bookid and (LendBookDate between '" + date1.Value.ToString() + "' and '" + date2.Value.ToString() + "')  order by BeBorrowTimes desc";
            SqlDataSource1.SelectCommand = sqlpa;


            SqlDataSource1.SelectParameters["name"].DefaultValue = "1";
            SqlDataSource1.SelectParameters["date1"].DefaultValue = date1.Value.ToString();
            SqlDataSource1.SelectParameters["date2"].DefaultValue = date2.Value.ToString();

        }
       

    }
    protected void baobiao_Click(object sender, EventArgs e)
    {

        SQL sql = new SQL();
        DataSet ds = sql.getDataset(sqlpa, "");
        DataTable dt = new DataTable();
        dt = ds.Tables[0];

        try
        {
            OutFileDao.OutFileToDisk(dt, "借阅排行", "F:\\数据库课设\\BookManage\\reports\\借阅排行.xls");
            Response.Write("<script language=javascript>alert('导出成功');window.location = 'LookBorrowedHistory.aspx';</script>");
        }
        catch (System.Exception ex)
        {

        }



    }


    protected void reader_Click(object sender, EventArgs e)
    {

        sqlpa = "select distinct top " + Convert.ToInt32(TextBox1.Text) + "  Book_info.BookID as 图书编号,Book_info.BookName as 图书条目,Book_info.Writer as 作者,BeBorrowTimes as 被借次数 from Reader_info, Book_info , Borrow_books_manage where Borrow_books_manage.bookid=Book_info.bookid and (LendBookDate between '" + date1.Value.ToString() + "' and '" + date2.Value.ToString() + "')  order by BeBorrowTimes desc ";
        SqlDataSource1.SelectCommand = sqlpa;


        SqlDataSource1.SelectParameters["name"].DefaultValue = "1";
        SqlDataSource1.SelectParameters["date1"].DefaultValue = date1.Value.ToString();
        SqlDataSource1.SelectParameters["date2"].DefaultValue = date2.Value.ToString();

    }

    protected void book_Click(object sender, EventArgs e)
    {

        sqlpa = "select Book_info.BookID as 图书编号,Book_info.BookName as 图书条目,Book_info.Writer as 作者,Book_info.BookPlace as 馆藏地点,Borrow_books_manage.LendBookDate as 借书日期, returndate as 还书时间, Borrow_books_manage.XUJIETIMES as 续借次数,readername as 读者姓名 from Reader_info, Book_info , Borrow_books_manage where Borrow_books_manage.bookid=Book_info.bookid and Reader_info.ReaderID=Borrow_books_manage.ReaderID and  (LendBookDate between '" + date1.Value.ToString() + "' and '" + date2.Value.ToString() + "') and ReturnDate is not null and book_info.bookID='" + TextBox1.Text + "' ";
        SqlDataSource1.SelectCommand = sqlpa;


        SqlDataSource1.SelectParameters["name"].DefaultValue = "1";
        SqlDataSource1.SelectParameters["date1"].DefaultValue = date1.Value.ToString();
        SqlDataSource1.SelectParameters["date2"].DefaultValue = date2.Value.ToString();

    }


    public string GetRandomColor()
    {
        Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
        // 对于C#的随机数，没什么好说的 
        System.Threading.Thread.Sleep(RandomNum_First.Next(50));
        Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);

        // 为了在白色背景上显示，尽量生成深色 
        int int_Red = RandomNum_First.Next(256);
        int int_Green = RandomNum_Sencond.Next(256);
        int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
        int_Blue = (int_Blue > 255) ? 255 : int_Blue;

        string str= System.Drawing.Color.FromArgb(int_Red, int_Green, int_Blue).Name;
        int count = 0;
        if (str.Length > 6)
        {
            count = str.Length - 6;
            str = str.Substring(count, 6);
        }
        return str;
    } 
}